package com.loadmore.baselibrary.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.loadmore.baselibrary.R;
import com.loadmore.baselibrary.util.LogUtil;
import com.loadmore.baselibrary.util.Util;
import com.loadmore.baselibrary.widget.CustomWindow;

public class ActivityInfoService extends Service {

    private boolean flag = true;
    private CustomWindow window;
    private Handler handler;
    private Thread thread;
    private int[] pos = new int[2];
    private PopupWindow popupWindow;
    private Button button;
    public ActivityInfoService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        window = new CustomWindow(getApplicationContext());
        final View view = window.setUpView(R.layout.activity_info_layout);
        final TextView title = (TextView) view.findViewById(R.id.title);
        window.show(30, 30);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                title.setText(Util.getTopActivity(getApplicationContext()));
               if (popupWindow == null) {
                   popupWindow = new PopupWindow(getApplicationContext());
                   popupWindow.setWidth(200);
                   popupWindow.setHeight(200);
                   View view1 = LayoutInflater.from(getApplicationContext()).inflate(R.layout.button,null);
                   Button button = (Button) view1.findViewById(R.id.button);
                   button.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           popupWindow.dismiss();
                       }
                   });
                   popupWindow.setContentView(view1);
               }
            }
        };
        thread = new Thread() {
            @Override
            public void run() {
                super.run();
                while (true) {
                    try {
                        Thread.sleep(3000);
                        LogUtil.e("HHHHHHH");
                        handler.sendEmptyMessage(0x01);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };
        thread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        window.hide();
    }

}
